<?php


namespace App\Http\Controllers\Admin\Access;


use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
//角色
class RoleController extends BaseController
{
    /**
     * @api {post} /access/role 添加角色
     *
     * @apiGroup 新权限模块
     *
     * @apiParam {string} role_name 角色名称，不超过10个字符，必须
     * @apiParam {string} role_desc 角色描述，不超过50个字符
     * @apiParam {int} status 状态，1：启用；2：禁用；必须
     * @apiParam {string} access_ids 给角色分配的权限id，多个之间用英文逗号隔开
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": [],
    "message": ""
    }
     */
    public function add(Request $request)
    {
        $param = $request->only('role_name', 'role_desc', 'status', 'access_ids');
        $v = app('validator')->make($param,[
            'role_name' => 'required|string|min:2|max:10',
            'role_desc' => 'string|max:50',
            'status' => 'required|integer|in:1,2',
            'access_ids' => 'string',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        app('repo_access')->addOrEditRole($param);
        return Response::sendData([]);
    }

    /**
     * @api {get} /access/role/detail 获取角色详情
     *
     * @apiGroup 新权限模块
     *
     * @apiParam {int} id 角色id，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "role_id": 23, 角色id
    "role_name": "言牟专用", 角色名称
    "role_memo": "言牟专用角色", 角色说明
    "status": 1, 状态：1：正常；2：禁用
    "access_ids": [ 角色拥有的权限id集合：
    2,
    4,
    5,
    6
    ]
    },
    "message": ""
    }
     */
    public function detail(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param,[
            'id' => 'required|integer',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $id = $param['id'];

        $detail = app('model_role')
            ->select('role_id', 'role_name', 'role_memo', 'status')
            ->where('role_id', $id)
            ->where('is_delete', 0)
            ->first();
        if (empty($detail)) throw new BasicException(0, '角色id不存在');

        $detail->access_ids = app('model_role_access_join')
            ->select('access_id')
            ->where('role_id', $id)
            ->pluck('access_id');

        return Response::sendData($detail);
    }

    /**
     * @api {put} /access/role 编辑角色
     *
     * @apiGroup 新权限模块
     *
     * @apiParam {int} id 角色id，必须
     * @apiParam {string} role_name 角色名称，不超过10个字符，必须
     * @apiParam {string} role_desc 角色描述，不超过50个字符
     * @apiParam {int} status 状态，1：启用；2：禁用；必须
     * @apiParam {string} access_ids 给角色分配的权限id，多个之间用英文逗号隔开
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": [],
    "message": ""
    }
     */
    public function edit(Request $request)
    {
        $param = $request->only('id', 'role_name', 'role_desc', 'status', 'access_ids');
        $v = app('validator')->make($param,[
            'id' => 'required|integer',
            'role_name' => 'required|string|min:2|max:10',
            'role_desc' => 'string|max:50',
            'status' => 'required|integer|in:1,2',
            'access_ids' => 'string',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        app('repo_access')->addOrEditRole($param);
        return Response::sendData([]);
    }

    /**
     * @api {get} /role/all 角色-获取所有角色（添加管理员时的角色下拉框）
     * @apiVersion 0.1.0
     * @apiName 角色-获取所有角色
     * @apiGroup 后台设置
     *
     * @apiSuccessExample 成功响应
     *
     * {
     * "code": 200,
     * "data": [{
     * "role_id(角色id)": 1,
     * "role_name(角色名称)": "超级管理员"
     * }, {
     * "role_id": 5,
     * "role_name": "超级管理员"
     * }]
     * }
     *
     */
    public function getRole()
    {
        $data = app('repo_permission')->getRole();

        return Response::sendData($data);
    }
}